package chapter_1.tcp_test;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;


/**
 * TCP服务端
 * @author fulv
 */
public class TCPServer {

    public static final int BUFSIZE = 6;
    public static void main(String[] args) throws IOException {
        //创建一个serverSocket实例，被动的侦听发送来的连接请求
        ServerSocket serverSocket = new ServerSocket(8080);

        /**
         * recvMsgSize:接收到的数据总长度
         * recvBuf:暂存接收到的字节数据
         */
        int recvMsgSize = 0;
        byte[] recvBuf = new byte[BUFSIZE];
        byte[] recvAllBuf = new byte[BUFSIZE];
        //cutOffSize保存上次接收到的字节数组最后一个字符被截断的长度,lastCharEncodeLength保存上次接收的最有一个字符的编码字节长度
        byte[] temp = new byte[2];
        int cutOffLength = 0;
        int lastCharEncodeLength = 0;
        int recvAllBufSize = 0;
        while(true){
            //获取发送连接的客户端的socket
            Socket clientSocket = serverSocket.accept();
            System.out.println("获取到来自"+clientSocket.getRemoteSocketAddress()+"的tcp连接");
            InputStream in = clientSocket.getInputStream();
            OutputStream out = clientSocket.getOutputStream();

            //获取从客户端发来的数据
//            while((recvMsgSize = in.read(recvBuf))!=-1){
//                //if(recvBuf[recvMsgSize-1] &)
//                //判断总数组是否需要扩容
//                if(recvMsgSize+recvAllBufSize>recvAllBuf.length){
//                    recvAllBuf = Utils.expandArray(recvAllBuf);
//                }
//                System.arraycopy(recvBuf, 0, recvAllBuf, recvAllBufSize + 0, recvMsgSize);
//            }
//
//            String msg = new String(recvAllBuf);
//            System.out.println("接收到来自"+clientSocket.getRemoteSocketAddress()+"的消息:"+msg);
//            out.write(recvAllBuf);

            while((recvMsgSize  = in.read(recvBuf)) !=-1){
                byte[] mergeByteArray = new byte[recvMsgSize+cutOffLength];
                if(cutOffLength != 0) {
                    System.arraycopy(temp, 2 - cutOffLength, mergeByteArray, 0, cutOffLength);
                }
                int lastCutOffLength = cutOffLength;
                int result = Utils.isCutOff(recvBuf[recvMsgSize - 3], recvBuf[recvMsgSize - 2], recvBuf[recvMsgSize - 1]);
                switch (result) {
                    case 3:
                        cutOffLength = 1;
                        lastCharEncodeLength = 2;
                        temp[1] = recvBuf[recvMsgSize-1];
                        break;
                    case 4:
                        cutOffLength = 1;
                        lastCharEncodeLength = 3;
                        temp[1] = recvBuf[recvMsgSize-1];
                        break;
                    case 5:
                        cutOffLength = 2;
                        lastCharEncodeLength = 3;
                        temp[0] = recvBuf[recvMsgSize-2];
                        temp[1] = recvBuf[recvMsgSize-1];
                        break;
                    default:
                        cutOffLength = 0;
                        lastCharEncodeLength = 0;
                }
                System.arraycopy(recvBuf, 0, mergeByteArray, lastCutOffLength, recvMsgSize - cutOffLength);
                System.out.println(new String(mergeByteArray));
            }

            clientSocket.close();
        }
    }
}
